package com.hmall.util;

import com.hmall.model.MallPaymentVoucher;
import com.hmall.vo.OrderFufillmentVo;
import com.hmall.vo.SendProductItem;
import com.hmall.vo.StatementOfAccountVo;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;


/**
 * @ClassName PdfUtil
 * @Description //
 * @Author zhoutao
 * @Date 2018/11/2 10:44
 * @Version 2.0
 **/
public class PdfUtil {
    public static BaseFont bfChinese = null;

    public static void createPDF(OrderFufillmentVo orderFufillmentVo) throws FileNotFoundException, DocumentException {

        Document document = new Document(PageSize.A4);
        String name = "pdf/"  + orderFufillmentVo.getOrderFufillmentName();
        PdfWriter.getInstance(document, new FileOutputStream(name));
        try{
            //linux和windows字体位置不同
//            bfChinese = BaseFont.createFont("/usr/share/fonts/lyx/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            Font font1 = setfont(20);
            Font font2 = setfont(12);
            document.addTitle("title");
            document.open();


            //标题名称
            Paragraph titleCustomerName = new Paragraph(orderFufillmentVo.getJiaInformation().getCustomerName(), font1);
            titleCustomerName.setAlignment(Element.ALIGN_CENTER);
            document.add(titleCustomerName);


            //发货单
            Paragraph titleSend = new Paragraph("发货单", font1);
            titleSend.setAlignment(Element.ALIGN_CENTER);
            document.add(titleSend);

            //加入空行
            Paragraph blankRow = new Paragraph(18f, " ");
            document.add(blankRow);

            //第一块内容
            Paragraph content1 = new Paragraph("致：" + orderFufillmentVo.getYiInformation().getCustomerName(), font2);
            content1.setAlignment(Element.ALIGN_LEFT);
            document.add(content1);

            //第二块内容
            Paragraph content2 = new Paragraph("    尊敬的客户，您好！以下为从" + orderFufillmentVo.getSendProduct().getStartTime() + "至" + orderFufillmentVo.getSendProduct().getEndTime()
                    + "贵司向我司购货、付款及欠款情况明细表，本对账单作为双方核账依据，为规范货款的结算管理，汇款时请按我司指定账户汇入：", font2);
            document.add(content2);


            //加入空行
            document.add(blankRow);
            //表格测试



            //表格
            PdfPTable table = new PdfPTable(9);
            //边线
            table.getDefaultCell().setBorder(1);
            table.setPaddingTop(8);
            table.setWidthPercentage(100);
            PdfPCell cell1 = new PdfPCell(createCell("我司向贵司发货明细"));
            cell1.setColspan(9);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell1);

            //产品信息
            table.addCell(createCell("日期"));
            table.addCell(createCell("单号"));
            table.addCell(createCell("类型"));
            table.addCell(createCell("规格"));
            table.addCell(createCell("批号"));
            table.addCell(createCell("等级"));
            table.addCell(createCell("数量"));
            table.addCell(createCell("单价"));
            table.addCell(createCell("金额"));


            List<SendProductItem> sendProductItemList = orderFufillmentVo.getSendProduct().getSendProductItemList();
            for(SendProductItem sendProductItem : sendProductItemList){
                table.addCell(createCell(sendProductItem.getDateTime()));
                table.addCell(createCell(sendProductItem.getOrderNo().toString()));
                table.addCell(createCell(sendProductItem.getType()));
                table.addCell(createCell(sendProductItem.getFineAndPore()));
                table.addCell(createCell(sendProductItem.getModel()));
                table.addCell(createCell(sendProductItem.getGrade()));
                table.addCell(createCell(sendProductItem.getWeight().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));
                table.addCell(createCell(sendProductItem.getUnitPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));
                table.addCell(createCell(sendProductItem.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));
            }

            //不足三个补齐
            int size = sendProductItemList.size();
            for(int i = 0 ; i < 3 - size; i ++){
                for(int j = 0; j <= 8; j++){
                    table.addCell(createCell(""));
                }
            }



            //金额统计
            PdfPCell cell2 = createCell("合计");
            cell2.setColspan(6);
//            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell2);

            //总重量
            table.addCell(createCell(orderFufillmentVo.getSendProduct().getAllWeight().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));

            table.addCell(createCell(""));

            //总金额
            table.addCell(createCell(orderFufillmentVo.getSendProduct().getAllTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));

            PdfPCell cell3 = createCell("贵司向我司付款明细");

            cell3.setColspan(9);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell3);

//            document.add(table);

            table.addCell(createCell("日期"));

            PdfPCell cell4 = createCell("付款时间及付款方式");
            cell4.setColspan(5);
            table.addCell(cell4);

            PdfPCell cell5 = createCell("金额");
            cell5.setColspan(3);
            table.addCell(cell5);

            List<MallPaymentVoucher> mallPaymentVoucherList = orderFufillmentVo.getMallPaymentVoucherList();

            Calendar calendar = Calendar.getInstance();
            for(MallPaymentVoucher mallPaymentVoucher : mallPaymentVoucherList){
                //创建时间————>用户提交时间
                //更新时间————>办事员审核时间
                calendar.setTime(mallPaymentVoucher.getCreateTime());
                String submitDate = "" + calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DATE);
                table.addCell(createCell(submitDate));

                //旧数据
                String agreeDate = "";
                if(null != mallPaymentVoucher.getUpdateTime()) {
                    calendar.setTime(mallPaymentVoucher.getUpdateTime());
                    agreeDate += calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DATE);
                }
                PdfPCell subCell1 = createCell(agreeDate + " 承兑付款");
                subCell1.setColspan(5);
                table.addCell(subCell1);

                PdfPCell subCell2 = createCell(mallPaymentVoucher.getFactPayment().toString());
                subCell2.setColspan(3);
                table.addCell(subCell2);
            }


            for(int i = 0 ; i < 3 - mallPaymentVoucherList.size(); i ++){
                table.addCell(createCell(""));

                PdfPCell subCell1 = createCell("");
                subCell1.setColspan(5);
                table.addCell(subCell1);

                PdfPCell subCell2 = createCell("");
                subCell2.setColspan(3);
                table.addCell(subCell2);
            }

            //合计
            PdfPCell cell6 = createCell("合计");
            cell6.setColspan(6);
            table.addCell(cell6);

            PdfPCell cell7 = createCell("");
            cell7.setColspan(3);
            table.addCell(cell7);

            PdfPCell cell8 = createCell("结欠情况");
            cell8.setRowspan(4);
            cell8.setColspan(1);
            table.addCell(cell8);

//
            String[] str = {"上月欠款", "本月货款", "本月收款" , "月末欠款"};
            StatementOfAccountVo statementOfAccountVo = orderFufillmentVo.getStatementOfAccountVo();
            BigDecimal[] price = {statementOfAccountVo.getLastMonthUnpayment(), statementOfAccountVo.getThisMonthFufillmentPayment(), statementOfAccountVo.getThisMonthReturnPayment(), statementOfAccountVo.getThisMonthUnpayment()};
            for(int i = 0; i <= 3; i++) {
                PdfPCell cell9 = createCell(str[i]);
                cell9.setColspan(5);
                table.addCell(cell9);

                PdfPCell cell10 = createCell(price[i].toString());
                cell10.setColspan(3);
                table.addCell(cell10);
            }

            document.add(table);
//
//            //第三块内容
            Paragraph content3 = new Paragraph("备注：请务必将核对结果确认后加盖公章或法人签字后，交由我司的销售人员或财务人员。如果在次月20日前未确认，我司将以此对账单为入账依据。谢谢您的支持与合作。", font2);
            content3.setAlignment(Element.ALIGN_LEFT);
            document.add(content3);
//
            //空行
            document.add(blankRow);
//            //表格
            PdfPTable table2 = new PdfPTable(4);
            table2.setWidthPercentage(100);
            table2.setHorizontalAlignment(Element.ALIGN_LEFT);
            PdfPCell pdfPCell11 = createCell2("银行账号");
            pdfPCell11.setRowspan(2);
            table2.addCell(pdfPCell11);

            table2.addCell(createCell2(orderFufillmentVo.getJiaInformation().getBankName()));
            table2.addCell(createCell3(orderFufillmentVo.getJiaInformation().getCustomerName()));
            PdfPCell pdfPCell12 = createCell3(orderFufillmentVo.getSendProduct().getEndTime());
            pdfPCell12.setRowspan(2);
            table2.addCell(pdfPCell12);

            table2.addCell(createCell2(orderFufillmentVo.getJiaInformation().getBankNumber()));
            table2.addCell(createCell3("电话" + orderFufillmentVo.getJiaInformation().getCustomerPhone()));
            document.add(table2);
            //公司名称

            for(int i = 0; i <= 2; i++) {
                document.add(blankRow);
            }

            PdfPTable table3 = new PdfPTable(2);
            table3.setWidthPercentage(100);
            table3.addCell(createCell4("相符"));
            table3.addCell(createCell4("不相符"));

            String tStr[] = {"签字", "盖章", "日期"};
            for(int i = 0; i <= 2; i++){
                for(int j = 0; j <= 1; j++){
                    table3.addCell(createCell4(tStr[i]));
                }
            }

            document.add(table3);

        }catch (Exception e){
            return;
        }

        document.close();
        return;
    }


    public static Font setfont(Integer size) {
        //中文字体,解决中文不能显示问题
        Font font = new Font(bfChinese);
        font.setSize(size);

        return font;
    }


    /**
     * @Date 2018/11/2 10:36
     * @Param [cell]
     * @return com.itextpdf.text.pdf.PdfPCell
     * @Description // 设置居中
     */
    private static PdfPCell setAlignment(PdfPCell cell){
        //水平居中
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        return cell;
    }

    /**
     * @Date 2018/11/2 10:40
     * @Param [name]
     * @return com.itextpdf.text.pdf.PdfPCell
     * @Description // 创建单元格
     */
    public static PdfPCell createCell(String name) {

        //8
        Font tableFont = setfont(7);
        PdfPCell cell = new PdfPCell(new Paragraph(name, tableFont));
        cell = setAlignment(cell);
        cell.setMinimumHeight(20);
        return cell;
    }

    /**
     * @Date 2018/11/2 10:40
     * @Param [name]
     * @return com.itextpdf.text.pdf.PdfPCell
     * @Description // 第二种单元格，类型少省的写
     */
    public static PdfPCell createCell2(String name) {

        Font tableFont = setfont(8);
        PdfPCell cell = new PdfPCell(new Paragraph(name, tableFont));
        cell = setAlignment(cell);
        cell.setMinimumHeight(25);
        return cell;
    }

    /**
     * @Date 2018/11/2 10:39
     * @Param [name]
     * @return com.itextpdf.text.pdf.PdfPCell
     * @Description // 创建无边框单元格
     */
    public static PdfPCell createCell3(String name){

        Font tableFont = setfont(8);
        PdfPCell cell = new PdfPCell(new Paragraph(name, tableFont));
        cell = setAlignment(cell);
        cell.setMinimumHeight(16);
        cell.setBorder(0);
        return cell;
    }

    /**
     * @Date 2018/11/2 10:40
     * @Param [name]
     * @return com.itextpdf.text.pdf.PdfPCell
     * @Description // 底下签字单元格
     */
    public static PdfPCell createCell4(String name){

        Font tableFont = setfont(10);
        PdfPCell cell = new PdfPCell(new Paragraph(name, tableFont));
        cell = setAlignment(cell);
        cell.setMinimumHeight(20);
        cell.setBorder(0);
        return cell;
    }

}
